package com.jindianjichi.subject.application.controller;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.google.common.base.Preconditions;
import com.jindianjichi.basic.entity.SubjectInfoEs;
import com.jindianjichi.subject.application.convert.SubjectAnswerDtoConverter;
import com.jindianjichi.subject.application.convert.SubjectInfoDtoConverter;
import com.jindianjichi.subject.application.entity.SubjectInfoDTO;
import com.jindianjichi.subject.common.entity.PageResult;
import com.jindianjichi.subject.common.entity.Result;
import com.jindianjichi.subject.domain.entity.SubjectAnswerBo;
import com.jindianjichi.subject.domain.entity.SubjectInfoBo;
import com.jindianjichi.subject.domain.service.SubjectInfoDomainService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

/**
 * 刷题Controller
 */
@RestController
@RequestMapping("/subject")
@Slf4j
public class SubjectController {
    @Resource
    private SubjectInfoDomainService subjectInfoDomainService;


    @PostMapping("/add")
    public Result<Boolean> add(@RequestBody SubjectInfoDTO subjectInfoDTO) {
        try {
            if (log.isInfoEnabled()){
                log.info("SubjectController.add.SubjectInfoDTO:{}", subjectInfoDTO);
            }
            Preconditions.checkNotNull(subjectInfoDTO.getSubjectName(),"题目名称不能为空");
            Preconditions.checkNotNull(subjectInfoDTO.getSubjectType(),"题目类型不能为空");
            Preconditions.checkNotNull(subjectInfoDTO.getSubjectScore(),"题目分数不能为空");
            Preconditions.checkNotNull(subjectInfoDTO.getSubjectParse(),"题目解析不能为空");
            Preconditions.checkArgument(!CollectionUtils.isEmpty(subjectInfoDTO.getCategoryIds())
                    , "分类id不能为空");
            Preconditions.checkArgument(!CollectionUtils.isEmpty(subjectInfoDTO.getLabelIds())
                    , "标签id不能为空");

            SubjectInfoBo subjectInfoBo = SubjectInfoDtoConverter.instance.convertToBoInfo(subjectInfoDTO);
            List<SubjectAnswerBo> subjectAnswerBos =
                    SubjectAnswerDtoConverter.instance.convertToBoList(subjectInfoDTO.getOptionList());
            subjectInfoBo.setOptionList(subjectAnswerBos);
            subjectInfoDomainService.addSubjectInfo(subjectInfoBo);
            return Result.ok(true);
        } catch (Exception e) {
            log.error("SubjectController.add.error:{}", e.getMessage(), e);
            return Result.fail(false);
        }

    }

    @PostMapping("/getSubjectPage")
    public Result<PageResult<SubjectInfoDTO>> getSubjectPage(@RequestBody SubjectInfoDTO subjectInfoDto){
        if (log.isInfoEnabled()){
            log.info("SubjectController.getSubjectPage.SubjectInfoDTO:{}", subjectInfoDto);
        }
        Preconditions.checkNotNull(subjectInfoDto.getCategoryId(),"分类id不能为空");
        Preconditions.checkNotNull(subjectInfoDto.getLabelId(),"标签id不能为空");
        SubjectInfoBo subjectInfoBo = SubjectInfoDtoConverter.instance.convertToBoInfo(subjectInfoDto);
        subjectInfoBo.setPageNo(subjectInfoDto.getPageNo());
        subjectInfoBo.setPageSize(subjectInfoDto.getPageSize());
         PageResult<SubjectInfoBo> subjectInfoBOPageResult=      subjectInfoDomainService.getSubjectPage(subjectInfoBo);
        PageResult<SubjectInfoDTO> subjectInfoDtoPageReResult = new PageResult<SubjectInfoDTO>();
        subjectInfoDtoPageReResult.setPageNo(subjectInfoBOPageResult.getPageNo());
        subjectInfoDtoPageReResult.setPageSize(subjectInfoBOPageResult.getPageSize());
        subjectInfoDtoPageReResult.setTotal(subjectInfoBOPageResult.getTotal());
        subjectInfoDtoPageReResult.setTotal(subjectInfoBOPageResult.getTotal());
        List<SubjectInfoDTO> subjectInfoDTOS = SubjectInfoDtoConverter.instance.convertToDtoInfo(subjectInfoBOPageResult.getResult());
        subjectInfoDtoPageReResult.setResult(subjectInfoDTOS);
        subjectInfoDtoPageReResult.setStart(subjectInfoBOPageResult.getStart());
        subjectInfoDtoPageReResult.setEnd(subjectInfoBOPageResult.getEnd());
                return Result.ok(subjectInfoDtoPageReResult);

    }

    @PostMapping("/querySubjectInfo")
    public Result<SubjectInfoDTO> querySubjectInfo(@RequestBody SubjectInfoDTO subjectInfoDto) {
        if (log.isInfoEnabled()) {
            log.info("SubjectController.getSubjectPage.SubjectInfoDTO:{}", subjectInfoDto);
        }
        Preconditions.checkNotNull(subjectInfoDto.getId(),"题目Id不能为空");
        //将dto转化为BO
        SubjectInfoBo subjectInfoBO = SubjectInfoDtoConverter.instance.convertToBoInfo(subjectInfoDto);
        SubjectInfoBo boResult = subjectInfoDomainService.querySubjectInfo(subjectInfoBO);
        SubjectInfoDTO dto = SubjectInfoDtoConverter.instance.convertToDto(boResult);
        return Result.ok(dto);

    }

    @PostMapping("/getContributeList")
    public Result<SubjectInfoDTO> getContributeList() {


        List<SubjectInfoBo>  boList = subjectInfoDomainService.getContributeList();
        List<SubjectInfoDTO> dtoList = SubjectInfoDtoConverter.instance.convertToDtoList(boList);
        return Result.ok(dtoList);

    }

    /**
     * 全文检索
     */
    @PostMapping("/getSubjectPageBySearch")
    public Result<PageResult<SubjectInfoEs>> getSubjectPageBySearch(@RequestBody SubjectInfoDTO subjectInfoDTO) {
        try {
            if (log.isInfoEnabled()) {
                log.info("SubjectController.getSubjectPageBySearch.dto:{}", JSON.toJSONString(subjectInfoDTO));
            }
            Preconditions.checkArgument(StringUtils.isNotBlank(subjectInfoDTO.getKeyWords()), "关键词不能为空");
            SubjectInfoBo subjectInfoBO = SubjectInfoDtoConverter.instance.convertToBoInfo(subjectInfoDTO);
            subjectInfoBO.setPageNo(subjectInfoDTO.getPageNo());
            subjectInfoBO.setPageSize(subjectInfoDTO.getPageSize());
            PageResult<SubjectInfoEs> boPageResult = subjectInfoDomainService.getSubjectPageBySearch(subjectInfoBO);
            return Result.ok(boPageResult);
        } catch (Exception e) {
            log.error("SubjectCategoryController.getSubjectPageBySearch.error:{}", e.getMessage(), e);
            return Result.fail("全文检索失败");
        }
    }

}